package com.hua.api.modules.job.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hua.api.core.framework.util.DateUtil;
import com.hua.api.core.out.amap.AmapHelper;
import com.hua.api.core.out.baidu.BaiduHelper;
import com.hua.api.core.properties.SysProperties;
import com.hua.api.modules.finance.domain.OrderBill;
import com.hua.api.modules.finance.service.OrderBillService;
import com.hua.api.modules.job.service.JobInfoService;
import com.hua.api.modules.message.service.SmsSenderService;
import com.hua.api.modules.order.dao.OrderInfoDao;
import com.hua.api.modules.order.dao.ShopInfoDao;
import com.hua.api.modules.order.domain.OrderInfo;
import com.hua.api.modules.order.domain.ShopInfo;
import com.hua.api.modules.order.domain.ShopInfoChannel;
import com.hua.api.modules.order.repository.ShopInfoRepository;
import com.hua.api.modules.order.service.OrderBidService;
import com.hua.api.modules.order.service.OrderInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class JobInfoServiceImpl implements JobInfoService {


    private final ShopInfoDao shopInfoDao;

    private final OrderInfoDao orderInfoDao;

    private final OrderInfoService orderInfoService;

    private final OrderBillService orderBillService;

    private final SysProperties sysProperties;

    private final OrderBidService orderBidService;

    private final SmsSenderService smsSenderService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void orderBillJob() {
        log.info("JobInfoServiceImpl.orderBillJob 订单结算开始");
        orderBillService.orderBillJob();
    }

    /**
     * 配送完成的订单，设置完 已完成 。
     * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void orderInfoStatusSync() {
        log.info("JobInfoServiceImpl.orderInfoStatusSync() 订单的状态同步， 配送完成的订单，设置为已完成！");
        Date endDate = DateUtil.offsetDate(new Date(),-7); // 7天前
        List<OrderInfo> list = orderInfoDao.queryNeedSetToDoneOrderInfo(endDate);
        log.info("JobInfoServiceImpl.orderInfoStatusSync() 订单完成任务 endDate={},size={}",DateUtil.getFormatDate(endDate,"yyyy-MM-dd HH:mm:ss"),list.size());
        for( OrderInfo orderInfo:list ){
            orderInfoService.onOrderInfoFinish(orderInfo.getOrderNo());
        }
        log.info("JobInfoServiceImpl.orderInfoStatusSync() 任务完成！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void orderInfoFixed() {
        log.info("JobInfoServiceImpl.orderInfoFixed() 订单信息补齐，计算配送距离，以及省市区信息！");
        Date beginDate = DateUtil.offsetDate(new Date(),-7);
        ShopInfoChannel shopInfoChannel = orderInfoDao.getObject(ShopInfoChannel.class,sysProperties.wechatChannelId);
        if(null!=shopInfoChannel){
            List<OrderInfo> list = orderInfoDao.queryNeedDistanceFixed(beginDate,shopInfoChannel.getShopId());
            log.info("JobInfoServiceImpl.orderInfoFixed() size={}",list.size());
            for(OrderInfo o:list){
                ShopInfo shop = orderInfoDao.getObject(ShopInfo.class,o.getShopId());
                if(null!=shop){
                    String origin = shop.getLongitude()+","+shop.getLatitude();
                    String destination = o.getLongitude()+","+o.getLatitude();
                    Long distance = AmapHelper.calculationDistance(origin,destination);
                    o.setDistance(distance.intValue());
                    orderInfoDao.updateObject(o);
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void shopInfoFixed() {
        Date beginDate = DateUtil.offsetDate(new Date(),-30);
        List<ShopInfo> list = shopInfoDao.queryNeedFixedShopInfo(beginDate);
        log.info("ShopInfoServiceImpl.shopInfoFixed list.size = {}" , list.size());
        for(ShopInfo shopInfo : list){
            JSONObject data = BaiduHelper.changeGeoToBaidu(shopInfo.getLatitude(), shopInfo.getLongitude());
            shopInfo.setBdLatitude(data.getDoubleValue("lat"));
            shopInfo.setBdLongitude(data.getDoubleValue("lng"));
            shopInfoDao.updateObject(shopInfo);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void orderBidStatusSync() {
        log.info("JobInfoServiceImpl.orderBidStatusSync() 订单转单状态同步！");
        orderBidService.setOrderBidFinishJob();
        log.info("JobInfoServiceImpl.orderBidStatusSync() 订单转单状态同步完成！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void smsSenderJob() {
        smsSenderService.smsSenderJob();

    }
}
